package games.checkers;

import games.checkers.gameColors.colorScheme;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class checkers extends JFrame implements WindowListener
{
	private gameColors gamecolors;
	private int currentColors;
	private int numberPlayers;
	private checkersControls controls;
	private checkersBoard board;
	private chipBin redBin;
	private chipBin whiteBin;
	private chipManager chipMan;
	private boardView previousMove;
	
	private Dimension progDims = new Dimension();
	
	@Override
	public void windowActivated(WindowEvent arg0) 
	{
	}

	@Override
	public void windowClosed(WindowEvent arg0) 
	{
	}

	@Override
	public void windowClosing(WindowEvent arg0) 
	{
		dispose();
        System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) 
	{
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) 
	{
	}

	@Override
	public void windowIconified(WindowEvent arg0) 
	{
	}

	@Override
	public void windowOpened(WindowEvent arg0) 
	{	
	}
	
	public checkers()
	{	
		numberPlayers = 0;
		currentColors = 0;
		gamecolors = new gameColors();
		
		addWindowListener(this);
		
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
		controls = new checkersControls(this);
		this.add(controls); 
		
		JPanel gameplay = new JPanel();
		gameplay.setLayout(new BoxLayout(gameplay, BoxLayout.X_AXIS));
		this.add(gameplay);
		
//		//create the board View for displaying previous move
//		previousMove = new boardView();
		
		//initialize chip manager
		redBin = new chipBin(this, chipManager.player1);
		whiteBin = new chipBin(this, chipManager.player2);
		chipMan = new chipManager(this);
		
		
		//add player1 chip bin
		gameplay.add(whiteBin);
		
		// add the checkers board
		board = new checkersBoard(this);
		gameplay.add(board);
		
		//add player2 chip bin
		gameplay.add(redBin); 
		
		Dimension controlDim = controls.getSize();
		Dimension boardDim = board.getSize();
		Dimension binDim = redBin.getSize();
		progDims.width = boardDim.width+3*binDim.width;
		progDims.height = boardDim.height+2*controlDim.height;

		//make the background brown
		Color bgColor = new Color(160,82,45);
		getContentPane().setBackground(bgColor);
		gameplay.setBackground(bgColor);
		board.setBackground(bgColor);
		redBin.setBackground(bgColor);
		whiteBin.setBackground(bgColor);
		
		setSize(progDims);
		setPreferredSize(progDims);
		setTitle("Checkers");
		
		//center on screen
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension program = this.getSize();
		this.setLocation((screen.width-program.width)/2,(screen.height-program.height)/2);
		
		//show the program
		setVisible(true);
		
	}
	
	public void newGame()
	{
		board.reset();
		chipMan.reset();
		redBin.reset();
		whiteBin.reset();
		repaint();
	}

	public checkersBoard getBoard()
	{
		return board;
	}
	
	public chipManager chipManager()
	{
		return chipMan;
	}
	
	public chipBin redBin()
	{
		return redBin;
	}
	
	public chipBin whiteBin()
	{
		return whiteBin;
	}
	
	public void setColorScheme(int s)
	{
		currentColors = s;
		chipMan.setColors(gamecolors.getScheme(currentColors));
		board.setColors(gamecolors.getScheme(currentColors));
	}
	
	public colorScheme colorScheme()
	{
		return gamecolors.getScheme(currentColors);
	}
	
	public void numberPlayers(int n)
	{
		numberPlayers = n;
	}
	
	public int numberPlayers()
	{
		return numberPlayers;
	}
	
	public void gameLevel(int g)
	{
		chipMan.gameAI(g);
	}
	
	public boardView miniBoard()
	{
		return previousMove;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		checkers c = new checkers();
	}
}
